/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.earthquake.secondarycharacteristics;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.GenericAnswerListBox;
import gov.fema.hazus.bdc.client.hazards.earthquake.EarthquakeConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.EarthquakeSecondaryCharacteristics;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;

public class EarthquakeSecondaryCharacteristicsForm extends AbstractForm implements BuildingUpdate {
	
	private Label horizontalShape;
	private Label verticalShape;
	private Label torsion;
	private Label softStory;
	private Label shortColumns;
	private Label pounding;
	private Label URMRetrofit;
	private Label ornamentation;
	
	// form fields
	private ShapeConfigurationListBox formHorizontalShape = new ShapeConfigurationListBox();
	private ShapeConfigurationListBox formVerticalShape = new ShapeConfigurationListBox();
	private GenericAnswerListBox formTorsion = new GenericAnswerListBox();
	private GenericAnswerListBox formSoftStory = new GenericAnswerListBox();
	private GenericAnswerListBox formShortColumns = new GenericAnswerListBox();
	private GenericAnswerListBox formPounding = new GenericAnswerListBox();
	private GenericAnswerListBox formURMRetrofit = new GenericAnswerListBox();
	private OrnamentationListBox formOrnamentation = new OrnamentationListBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(8,2);
	
	// entities
	private EarthquakeSecondaryCharacteristics earthquakeSecondaryCharacteristics;
	
	// listeners
	private EarthquakeSecondaryCharacteristicsListBoxChangeListener earthquakeSecondaryCharacteristicsListBoxChangeListener;
	private EarthquakeConstants constants;
	
	public EarthquakeSecondaryCharacteristicsForm(Building building)
	{
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (EarthquakeConstants)GWT.create(EarthquakeConstants.class);
		tabTitle = constants.earthquakeSecondaryCharacteristicsTabTitle();
		
		horizontalShape = new Label(constants.earthquakeSecondaryCharacteristicsHorizontalShape());
		verticalShape = new Label(constants.earthquakeSecondaryCharacteristicsVerticalShape());
		torsion = new Label(constants.earthquakeSecondaryCharacteristicsTorsion());
		softStory = new Label(constants.earthquakeSecondaryCharacteristicsSoftStory());
		shortColumns = new Label(constants.earthquakeSecondaryCharacteristicsShortColumns());
		pounding = new Label(constants.earthquakeSecondaryCharacteristicsPounding());
		URMRetrofit = new Label(constants.earthquakeSecondaryCharacteristicsURMRetrofit());
		ornamentation = new Label(constants.earthquakeSecondaryCharacteristicsOrnamentation());
		
		// add labels to form
		table.setWidget(0, 0, horizontalShape);
		table.setWidget(1, 0, verticalShape);
		table.setWidget(2, 0, torsion);
		table.setWidget(3, 0, softStory);
		table.setWidget(4, 0, shortColumns);
		table.setWidget(5, 0, pounding);
		table.setWidget(6, 0, URMRetrofit);
		table.setWidget(7, 0, ornamentation);
		
		// set up form fields
		earthquakeSecondaryCharacteristics = new EarthquakeSecondaryCharacteristics();
		earthquakeSecondaryCharacteristicsListBoxChangeListener = new EarthquakeSecondaryCharacteristicsListBoxChangeListener(earthquakeSecondaryCharacteristics);

		// forms
		loadForms();
		
		// listeners
		loadListeners();
		


		// add fields to table
		table.setWidget(0, 1, formHorizontalShape);
		table.setWidget(1, 1, formVerticalShape);
		table.setWidget(2, 1, formTorsion);
		table.setWidget(3, 1, formSoftStory);
		table.setWidget(4, 1, formShortColumns);
		table.setWidget(5, 1, formPounding);
		table.setWidget(6, 1, formURMRetrofit);
		table.setWidget(7, 1, formOrnamentation);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);
	}


	public void updateBuilding(Building building) {
		
		// entities
		setBuilding(building);
		this.earthquakeSecondaryCharacteristics = building.getEarthquakeSecondaryCharacteristics();
		
		// listeners
		earthquakeSecondaryCharacteristicsListBoxChangeListener.updateEarthquakeSecondaryCharacteristics(earthquakeSecondaryCharacteristics);
		
		// reload forms
		loadForms();
		
	}
	
	private void loadForms()
	{
		if( earthquakeSecondaryCharacteristics != null )
		{
			formHorizontalShape.setListItem( earthquakeSecondaryCharacteristics.getHorizontalShape() );
			formHorizontalShape.setName(EarthquakeSecondaryCharacteristics.HORIZONTAL);
			
			formVerticalShape.setListItem( earthquakeSecondaryCharacteristics.getVerticalShape() );
			formVerticalShape.setName(EarthquakeSecondaryCharacteristics.VERTICAL);
			
			formTorsion.setListItem( earthquakeSecondaryCharacteristics.getTorsion() );
			formTorsion.setName(EarthquakeSecondaryCharacteristics.TORSION);
			
			formSoftStory.setListItem( earthquakeSecondaryCharacteristics.getSoftStory() );
			formSoftStory.setName(EarthquakeSecondaryCharacteristics.SOFTSTORY);
			
			formShortColumns.setListItem( earthquakeSecondaryCharacteristics.getShortColumns() );
			formShortColumns.setName(EarthquakeSecondaryCharacteristics.SHORTCOLUMNS);
			
			formPounding.setListItem( earthquakeSecondaryCharacteristics.getPounding() );
			formPounding.setName(EarthquakeSecondaryCharacteristics.POUNDING);
			
			formURMRetrofit.setListItem( earthquakeSecondaryCharacteristics.getURMRetrofit() );
			formURMRetrofit.setName(EarthquakeSecondaryCharacteristics.URMRETROFIT);
			
			formOrnamentation.setListItem( earthquakeSecondaryCharacteristics.getOrnamentation() );
			formOrnamentation.setName(EarthquakeSecondaryCharacteristics.ORNAMENTATION);
		}
		
		if( building != null)
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formHorizontalShape.setEnabled(false);
	            formVerticalShape.setEnabled(false);
	            formTorsion.setEnabled(false);
	            formSoftStory.setEnabled(false);
	            formShortColumns.setEnabled(false);
	            formPounding.setEnabled(false);
	            formURMRetrofit.setEnabled(false);
	            formOrnamentation.setEnabled(false);
	        }        
	        else
	        {
	            formHorizontalShape.setEnabled(true);
	            formVerticalShape.setEnabled(true);
	            formTorsion.setEnabled(true);
	            formSoftStory.setEnabled(true);
	            formShortColumns.setEnabled(true);
	            formPounding.setEnabled(true);
	            formURMRetrofit.setEnabled(true);
	            formOrnamentation.setEnabled(true);            
	        }
		}
	}
	
	private void loadListeners()
	{
		formHorizontalShape.addChangeListener(earthquakeSecondaryCharacteristicsListBoxChangeListener);
		formVerticalShape.addChangeListener(earthquakeSecondaryCharacteristicsListBoxChangeListener);
		formTorsion.addChangeListener(earthquakeSecondaryCharacteristicsListBoxChangeListener);
		formSoftStory.addChangeListener(earthquakeSecondaryCharacteristicsListBoxChangeListener);
		formShortColumns.addChangeListener(earthquakeSecondaryCharacteristicsListBoxChangeListener);
		formPounding.addChangeListener(earthquakeSecondaryCharacteristicsListBoxChangeListener);
		formURMRetrofit.addChangeListener(earthquakeSecondaryCharacteristicsListBoxChangeListener);
		formOrnamentation.addChangeListener(earthquakeSecondaryCharacteristicsListBoxChangeListener);
	}

}
